<?php

namespace App\Listeners;

use App\Events\OrderPaidEvent;
use App\Services\CountProductSales\CountProductSales;
use App\Supports\Product\ProductReturnBalance;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\RelationUser;
use App\Models\User;
use App\Services\FinishGroup\FinishGroup;

class OrderPaidListener
{
    public function handle(OrderPaidEvent $event)
    {
        $order = $event->order;
        $user = $order->user;

	    app(CountProductSales::class)->count($order);


       /* if($user->isNotValidVip()){

            return false;
        }*/

        if ($order->sid > 0) {
            
            //if($user->grade !="vip"){
                account_rewards($user)->plusOwnOrderRewards($order->items()->first()->sku->own_rewards * $order->items()->first()->amount, ['order_id' => $order->id]);
            //}
           
            if($user->parent){
                 account_rewards($user->parent)->plusFirstLevelOrderRewards($order->items()->first()->sku->parent_rewards  * $order->items()->first()->amount,$user->name, ['order_id' => $order->id,'children_id'=>$user->id]);

            }
            $orderNum = $user->order_num + 2; 
            $userLevel = !empty($orderNum % cs('sale')->give_team_levels) ? $orderNum % cs('sale')->give_team_levels : cs('sale')->give_team_levels;

            $parentUid = RelationUser::where('user_id',$user->id)->where('team_level',$userLevel)->first();
            if($parentUid){

                $parentUser = User::find($parentUid->parent_id);
                if($parentUser){
                    if($parentUser->grade =="commander"){
                        $rewards =$order->items()->first()->sku->team_rewards * cs('sale')->order_commander_rewards_percent / 100  * $order->items()->first()->amount;
                        account_rewards($parentUser)->plusSecondLevelOrderRewards($rewards,$user->name, ['order_id' => $order->id,'children_id'=>$user->id]);

                    }elseif($parentUser->grade =="senior_commander"){

                         $rewards = $order->items()->first()->sku->team_rewards * cs('sale')->order_senior_commander_rewards_percent / 100  * $order->items()->first()->amount;
                        account_rewards($parentUser)->plusSecondLevelOrderRewards($rewards,$user->name, ['order_id' => $order->id,'children_id'=>$user->id]);
                    }
                }
            }

        }elseif($order->group_id > 0){
            $count = $order->group->records()->count();

            if($count <=10 ){
                $records = $order->group->records()->create([
                    'user_id'=>$order->user_id,
                ]);

                if($count ==($order->group->product->group_number -1)){

                     app(FinishGroup::class, [
                        'group' => $order->group
                    ])->finishByJob();
                }
            }
            

            $number = $order->group->product->group_cards_number;
            if($order->user->grade == 'vip'){
                 account_group_cards($order->user)->plusGroupCards($number);
            }
           
           
           $orderNum = $user->order_num + 2; 
            $userLevel = !empty($orderNum % cs('sale')->give_team_levels) ? $orderNum % cs('sale')->give_team_levels : cs('sale')->give_team_levels;

            $userLevel = $userLevel+1;
            $parentUid = RelationUser::where('user_id',$user->id)->where('team_level',$userLevel)->first();
            if($parentUid){

                $parentUser = User::find($parentUid->parent_id);
                if($parentUser){
                   // if($parentUser->grade =="commander" || $parentUser->grade =="senior_commander" ){
                        $rewards =$order->items()->first()->sku->group_rewards * cs('sale')->order_senior_commander_rewards_percent / 100  * $order->items()->first()->amount;
                        account_balances($parentUser)->plusOrderRewards($rewards,$user->name, ['order_id' => $order->id,'children_id'=>$user->id]);

                    //}
                }
            }

        }

        $user->update(['order_num'=>$user->order_num + 1]);
        if($user->parent){
            user_upgrade($user->parent);

        }

    }
}
